package com.laot.searchManage.utils;

import com.laot.searchManage.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.laot.seacher_master.model.common.ErrorCode;
import org.laot.seacher_master.model.entity.User;
import org.laot.seacher_master.model.exception.BusinessException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Component
public class UserUtils {
    @Resource
    private JwtUtils jwtUtils;
    @Resource
    private UserService userService;
    @Value("${jwt.tokenHeader}")
    private String headerStr;

    public User getLoginUser(HttpServletRequest request){
        String token = request.getHeader(headerStr);
        String userAccountFromToken = jwtUtils.getUserAccountFromToken(token);
        if(StringUtils.isBlank(userAccountFromToken)) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        User userInfo = userService.getInfoByAccount(userAccountFromToken);
        if(userInfo == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        return userInfo;
    }


}
